from time import sleep
from selenium import webdriver
driver = webdriver.Firefox()

# 打开百度页面
driver.get("https://www.baidu.com")

# 找到百度首页的输入框
# element:页面元素
key_word = driver.find_element_by_id('kw')

# 输入查找网易邮箱

key_word.send_keys('网易邮箱登录')

# 找到搜索（定位）百度一下这个按钮

bt = driver.find_element_by_id('su')

bt.click()

# 停顿一下
sleep(2)

# 点第一个连接
driver.find_element_by_partial_link_text('163网易免费邮--中文').click()

# 停顿一下
sleep(2)

hds = driver.window_handles
driver.switch_to.window(hds[1])

# 停顿一下
sleep(5)

# 输入账号密码
driver.switch_to.frame(driver.find_element_by_xpath('//div[@id="loginDiv"]/iframe'))
driver.find_element_by_name('email').send_keys('hejiaqi19990403@163.com')
driver.find_element_by_name('password').send_keys('********')
# 登录
driver.find_element_by_partial_link_text('录').click()

sleep(4)
# 从iframe退出来
driver.switch_to.default_content()

sleep(2)
# 写信css定位
driver.find_element_by_css_selector('html body.ua-notIE.skyblue.ua-firefox.frame-default.frame-auto div#dvNavContainer.frame-nav nav.tT0 div#dvNavTop.tx0 ul#_mail_component_20_20.js-component-component.tJ0 li#_mail_component_24_24.js-component-component.ra0.mD0 span.om0 b.nui-ico.fn-bg.ga0').click()

sleep(2)

# 收件人
driver.find_element_by_class_name('nui-editableAddr-ipt').send_keys('ma_linpop@163.com')

sleep(2)

# xpath输入主题
driver.find_element_by_xpath("//input[@class='nui-ipt-input' and @type='text' and @maxlength='256']").send_keys('化工学院_计171班_何佳琪  自动化发送邮件')
sleep(2)

# 内容
xpath = driver.find_element_by_xpath("//div[@class='APP-editor-edtr']/iframe")

driver.switch_to_frame(xpath)

driver.find_element_by_xpath("//body[@class='nui-scroll' and @contenteditable='true']").send_keys('吃葡萄不吐葡萄皮！！！')

# 退出

driver.find_element_by_xpath("//div[@class='nui-toolbar-item']/div/span[2]").click()

# 停顿一下
sleep(12)

# 关闭浏览器
driver.quit()